package com.powerteam.controller.sys;

import com.powerteam.controller.AuthorizedController;
import com.powerteam.model.sys.Menu;
import com.powerteam.model.sys.Role;
import com.powerteam.service.sys.MenuService;
import com.powerteam.vo.Result;
import com.powerteam.vo.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 菜单页面相关接口
 *
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2021/12/30
 */
@Slf4j
@Controller
@RequestMapping("/menu")
public class MenuController extends AuthorizedController {

    @Autowired
    private MenuService menuService;

    /**
     * 菜单页
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String menu() {

        return "sys/menu";
    }

    /**
     * 查询所有菜单
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findAllMenu", method = RequestMethod.POST)
    public List<Menu> findAllMenu() {

        return menuService.findAllMenu();
    }

    /**
     * 查询该用户的菜单信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findUserMenuTree", method = RequestMethod.POST)
    public List<TreeNode<Menu>> findUserMenuTree() {

        return menuService.findUserMenuTree(super.getUser().getUserId());
    }

    /**
     * 查找所有菜单树
     *
     * @param
     * @return List<TreeNode < Menu>>
     * @Author jinming.zhang
     * @Date 2022/1/11
     */
    @ResponseBody
    @RequestMapping(value = "/findAllMenuTree", method = RequestMethod.POST)
    public List<TreeNode<Menu>> findAllMenuTree() {

        return menuService.findAllMenuTree();
    }

    /**
     * 查询某角色的菜单列表
     *
     * @param role
     * @return List<Menu>
     * @Author jinming.zhang
     * @Date 2022/1/11
     */
    @ResponseBody
    @RequestMapping(value = "/findRoleMenu", method = RequestMethod.POST)
    public List<Menu> findRoleMenu(@RequestBody Role role) {

        return menuService.findRoleMenu(role.getRoleId());
    }

    /**
     * 检查菜单名称是否重复
     *
     * @param menu
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/11
     */
    @ResponseBody
    @RequestMapping(value = "/checkMenuName", method = RequestMethod.POST)
    public Result checkMenuName(@RequestBody Menu menu) {

        menu.setMenuCode(null);
        return menuService.checkMenu(menu);
    }

    /**
     * 检查菜单编码是否重复
     *
     * @param menu
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/11
     */
    @ResponseBody
    @RequestMapping(value = "/checkMenuCode", method = RequestMethod.POST)
    public Result checkMenuCode(@RequestBody Menu menu) {

        menu.setMenuName(null);
        return menuService.checkMenu(menu);
    }

    /**
     * 添加菜单
     *
     * @param menu
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/11
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Menu menu) {

        return menuService.insert(menu);
    }

    /**
     * 删除菜单
     *
     * @param menu
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/11
     */
    @ResponseBody
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result remove(@RequestBody Menu menu) {

        return menuService.delete(menu.getMenuId());
    }

    /**
     * 根据菜单Id获取菜单信息
     *
     * @param menu
     * @return Menu
     * @Author jinming.zhang
     * @Date 2022/1/11
     */
    @ResponseBody
    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    public Menu findById(@RequestBody Menu menu) {

        return menuService.findById(menu.getMenuId());
    }

    /**
     * 修改菜单信息
     *
     * @param menu
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/11
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Menu menu) {

        return menuService.update(menu);
    }

    /**
     * 上调菜单
     *
     * @param menu
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/11
     */
    @ResponseBody
    @RequestMapping(value = "/up", method = RequestMethod.POST)
    public Result up(@RequestBody Menu menu) {

        return menuService.up(menu.getMenuId());
    }

    /**
     * 下调菜单
     *
     * @param menu
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/11
     */
    @ResponseBody
    @RequestMapping(value = "/down", method = RequestMethod.POST)
    public Result down(@RequestBody Menu menu) {

        return menuService.down(menu.getMenuId());
    }

}
